/*
 * @lc app=leetcode.cn id=141 lang=typescript
 *
 * [141] 环形链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

//  思路：快慢指针
//  如果快慢指针相遇，说明有环
function hasCycle(head: ListNode | null): boolean {
    let fast = head, slow = head
    while (fast && fast.next && slow && slow.next) {
        slow = slow.next
        fast = fast.next.next
        if (fast === slow) {
            return true
        }
    }
    return false
};
// @lc code=end
import { ListNode } from './type'

let head = new ListNode(3)
let node2 = new ListNode(2)
let node3 = new ListNode(0)
let node4 = new ListNode(-4)

head.next = node2
node2.next = node3
node3.next = node4
node4.next = node2

console.log(hasCycle(head));
